import Vue from 'vue'
import VueRouter from 'vue-router'
import tabBarLayout from '@/layout/tabBar'

Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

const routes = [
  {
    path: '/',
    component: tabBarLayout,
    children: [
      {
        path: '/',
        redirect: '/index',
      },
      {
        path: '/index',
        component: () => import('@/views/index'),
        meta: {
          title: '易卡拉权益',
        },
      },
      {
        path: '/orderCenter',
        component: () => import('@/views/orderCenter'),
        meta: {
          title: '订单中心',
        },
      },
      {
        title: '个人中心',
        path: '/myCenter',
        component: () => import('@/views/myCenter'),
        meta: {
          title: '个人中心',
        },
      },
      {
        title: '提现',
        path: '/withdraw',
        component: () => import('@/views/withdraw'),
        meta: {
          title: '提现',
        },
      },
      {
        title: '微信解绑',
        path: '/wxrelieve',
        component: () => import('@/views/wxrelieve'),
        meta: {
          title: '微信解绑',
        },
      },
      {
        title: '微信授权',
        path: '/authorization',
        component: () => import('@/views/authorization'),
        meta: {
          title: '微信授权',
        },
      },
      {
        title: '资金流水',
        path: '/fundflow',
        component: () => import('@/views/fundflow'),
        meta: {
          title: '资金流水',
        },
      },
      {
        title: '纠纷订单',
        path: '/dispute/:orderNo',
        component: () => import('@/views/dispute'),
        meta: {
          title: '纠纷订单',
        },
      },
    ],
  },
  {
    path: '/sellGoods',
    component: () => import('@/views/sellGoods'),
    meta: {
      title: '商品出售',
    },
  },
  {
    title: '搜索',
    path: '/search',
    component: () => import('@/views/search'),
    meta: {
      title: '搜索',
    },
  },
  {
    title: '手机注册',
    path: '/login',
    meta: {
      title: '登录易卡拉',
    },
    component: () => import('@/views/login'),
  },
  {
    title: '手机验证码登录',
    path: '/phonelogin',
    meta: {
      title: '登录易卡拉',
    },
    component: () => import('@/views/login/phoneLogin.vue'),
  },
  {
    title: '修改密码',
    path: '/forgetpw',
    meta: {
      title: '修改密码',
    },
    component: () => import('@/views/login/forgetpw.vue'),
  },
  {
    title: '申请入驻',
    path: '/apply',
    meta: {
      title: '申请入驻',
    },
    component: () => import('@/views/login/apply.vue'),
  },
]

const router = new VueRouter({
  routes,
})

router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

export default router
